package com.lottery.www.common.result;

import lombok.Data;
import lombok.ToString;
import org.springframework.http.HttpStatus;

/**
 * @author XiongJinChen
 * @version 1.0
 * @date 2020/5/7 19:16
 * @name Result
 */
@Data
@ToString
public class Result {
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应状态码说明
     */
    private String message;
    /**
     * 响应数据
     */
    private Object data;

    private Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private Result(Integer code) {
        this.code = code;
    }

    /**
     * 请求成功 ，不返回数据
     */
    public static Result success() {
        return new Result(StateCode.SUCCESS.getCode(), StateCode.SUCCESS.getMessage(),null);
    }

    public static Result success(Object data) {
        return new Result(StateCode.SUCCESS.getCode(),  StateCode.SUCCESS.getMessage(), data);
    }

    public static Result success(StateCode stateCode, Object data) {
        return new Result(stateCode.SUCCESS.getCode(),  stateCode.SUCCESS.getMessage(), data);
    }


    public static Result error() {
        return new Result( StateCode.SYSTEM_ERROR.getCode(),  StateCode.SYSTEM_ERROR.getMessage(), null);
    }

    public static Result error(StateCode stateCode) {
        return new Result(stateCode.getCode(), stateCode.getMessage(), null);
    }


    public static Result error(Integer code, String description, Object data) {
        return new Result(code, description, data);
    }

    public static Result error(HttpStatus httpStatus) {
        return new Result(httpStatus.value());
    }

    /**
     * 自定义错误信息
     */
    public static Result error(Integer code, String message) {
        return new Result(code, message, null);
    }
}
